﻿using SD.Infrastructure.Repository.EntityFrameworkCore;
using SD.Infrastructure.RepositoryBase;
using SD.Toolkits.EntityFrameworkCore.Extensions;
using SlamDunk.MES.Domain.Entities.ExecutionContext;
using SlamDunk.MES.Domain.IRepositories.Interfaces.ExecutionContext;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SlamDunk.MES.Repository.Implements.ExecutionContext
{
    /// <summary>
    /// 工单仓储实现
    /// </summary>
    public class WorkOrderRepository : EFAggRootRepositoryProvider<WorkOrder>, IWorkOrderRepository
    {
        #region # 分页获取工单列表 —— ICollection<WorkOrder> FindByPage(string keywords, Guid? productionPlanId...
        /// <summary>
        /// 分页获取工单列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="productionPlanId">生产计划Id</param>
        /// <param name="executionPlanId">作业计划Id</param>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <param name="workStationId">工位Id</param>
        /// <param name="needToInspect">是否需质检</param>
        /// <param name="hasInspected">是否已质检</param>
        /// <param name="dispatchedTeamId">分派班组Id</param>
        /// <param name="dispatchedPersonId">分派人员Id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="statuses">工单状态集</param>
        /// <param name="facilityIds">工厂模型Id集</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>工单列表</returns>
        public ICollection<WorkOrder> FindByPage(string keywords, Guid? productionPlanId, Guid? executionPlanId, Guid? processSegmentId, Guid? workStationId, bool? needToInspect, bool? hasInspected, Guid? dispatchedTeamId, Guid? dispatchedPersonId, DateTime? startTime, DateTime? endTime, IList<WorkOrderStatus> statuses, IList<Guid> facilityIds, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            Expression<Func<WorkOrder, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(keywords) || x.Keywords.Contains(keywords)) &&
                    (productionPlanId == null || x.ProductionPlanId == productionPlanId) &&
                    (executionPlanId == null || x.ExecutionPlanId == executionPlanId) &&
                    (processSegmentId == null || x.ProcessSegmentId == processSegmentId) &&
                    (workStationId == null || x.WorkStationId == workStationId) &&
                    (needToInspect == null || x.NeedToInspect == needToInspect) &&
                    (hasInspected == null || x.HasInspected == hasInspected) &&
                    (dispatchedTeamId == null || x.DispatchedTeamId == dispatchedTeamId) &&
                    (dispatchedPersonId == null || x.DispatchedPersonId == dispatchedPersonId) &&
                    (startTime == null || x.AddedTime >= startTime) &&
                    (endTime == null || x.AddedTime <= endTime);

            IQueryable<WorkOrder> workOrders = base.Find(condition);
            if (statuses != null && statuses.Any())
            {
                QueryBuilder<WorkOrder> statusConditionBuilder = QueryBuilder<WorkOrder>.Negate();
                foreach (WorkOrderStatus status in statuses)
                {
                    statusConditionBuilder.Or(x => x.Status == status);
                }
                workOrders = workOrders.Where(statusConditionBuilder.Build());
            }
            if (facilityIds != null && facilityIds.Any())
            {
                workOrders = workOrders.Where(x => x.FacilityId.HasValue && facilityIds.Contains(x.FacilityId.Value));
            }

            IOrderedQueryable<WorkOrder> orderedResult = workOrders.OrderByDescending(x => x.AddedTime);
            IQueryable<WorkOrder> pagedResult = orderedResult.ToPage(pageIndex, pageSize, out rowCount, out pageCount);

            return pagedResult.ToList();
        }
        #endregion
    }
}
